//
//  EFGoodsDetailViewCellSale.m
//  EnjogFood
//
//  Created by guocheng on 2017/6/11.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodsDetailViewCellSale.h"
#import "EFGoodsDetailViewItemSale.h"

@interface EFGoodsDetailViewCellSale()

@property(strong,nonatomic)UILabel* priceLabel;
@property(strong,nonatomic)UILabel* salePriceLabel;
@property(strong,nonatomic)UILabel* endTipsLabel;
@property(strong,nonatomic)UILabel* endTimeLabel;

@property(strong,nonatomic)NSTimer* endTimeTimer;
@property(assign,nonatomic)long endTime;

@end

@implementation EFGoodsDetailViewCellSale


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)dealloc{
    if(self.endTimeTimer){
        [self.endTimeTimer invalidate];
        self.endTimeTimer = nil;
    }
}


-(void)setObject:(EFGoodsDetailViewItemSale *)item{
    NSMutableAttributedString * strPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@ 限时秒杀价",item.salePrice]];
    
    NSRange range = NSMakeRange([[strPrice string] rangeOfString:@"限"].location, 5);
    [strPrice addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:range];
    self.priceLabel.attributedText = strPrice;
    
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};;
    NSString* strSalePrice = [NSString stringWithFormat:@"¥%@",item.price];
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:strSalePrice attributes:attribtDic];
    self.salePriceLabel.attributedText = attribtStr;
    
    self.endTime = item.endTime;
    [self.endTipsLabel setText:@"距离结束"];
    
    [self.priceLabel sizeToFit];
    self.priceLabel.left = 20;
    self.priceLabel.top = 10;
    
    [self.salePriceLabel sizeToFit];
    self.salePriceLabel.left = 20;
    self.salePriceLabel.top = self.priceLabel.bottom + 7;
    
    [self.endTipsLabel sizeToFit];
    self.endTipsLabel.top = 10;
    self.endTipsLabel.left = kScreenWidth - self.endTipsLabel.width - 20;
    
    
    long now = time(NULL);
    NSTimeInterval balance =  self.endTime  - now;
    if(balance < 0){
        NSString* endTimeStr = [NSString stringWithFormat:@"00:00:00"];
        [self.endTimeLabel setText:endTimeStr];
        
        [self.endTimeLabel sizeToFit];
        self.endTimeLabel.top = self.endTipsLabel.bottom + 7;
        self.endTimeLabel.left = kScreenWidth - self.endTimeLabel.width - 20;
        return;
    }
    
    [self setEndTime];
    self.endTimeTimer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setEndTime) userInfo:NULL repeats:YES  ];
     
     //    [NSTimer timerWithTimeInterval:1 repeats:YES
//                             block:^(NSTimer * _Nonnull timer) {
//                                 [self setEndTime];
//                             }];
    
}
-(void)setEndTime{
    
    long now = time(NULL);
    NSTimeInterval balance =  self.endTime  - now;
    if(balance < 0)
        return;
    NSString*timeString = [[NSString alloc]init];
    timeString = [NSString stringWithFormat:@"%f",balance ];
    //timeString = [NSString stringWithFormat:@"%f",balance /60];
    //timeString = [timeString substringToIndex:timeString.length - 7];
    
    NSInteger timeInt = [timeString intValue];
    NSInteger day = timeInt /60/60/24;
    NSInteger hour = (timeInt - (day*60*60*24))/60/60;
    NSInteger mint = (timeInt/60) % 60;
    NSInteger second = timeInt % 60;
    NSString* dayStr = [NSString stringWithFormat:@"%ld",day];
    NSMutableString* hourStr = [NSMutableString stringWithString:@"00"];
    if(hour > 0){
        if(hour < 10){
            hourStr = [NSMutableString stringWithFormat:@"0%ld",hour];
        }else{
            hourStr = [NSMutableString stringWithFormat:@"%ld",hour];
        }
    }
    NSMutableString* minuStr = [NSMutableString stringWithString:@"00"];
    if(mint > 0){
        if(mint < 10){
            minuStr = [NSMutableString stringWithFormat:@"0%ld",mint];
        }else{
            minuStr = [NSMutableString stringWithFormat:@"%ld",mint];
        }
    }
    
    NSMutableString* secondStr = [NSMutableString stringWithString:@"00"];
    if(second > 0){
        if(second < 10){
            secondStr = [NSMutableString stringWithFormat:@"0%ld",second];
        }else{
            secondStr = [NSMutableString stringWithFormat:@"%ld",second];
        }
    }
    NSString* endTimeStr = [NSString stringWithFormat:@"%@天%@小时%@分%@秒",dayStr,hourStr,minuStr,secondStr];
    [self.endTimeLabel setText:endTimeStr];
    
    [self.endTimeLabel sizeToFit];
    self.endTimeLabel.top = self.endTipsLabel.bottom + 7;
    self.endTimeLabel.left = kScreenWidth - self.endTimeLabel.width - 20;
}


-(void)initView{
    [self.contentView setBackgroundColor:RGBCOLOR(0xcda366)];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.salePriceLabel];
    [self.contentView addSubview:self.endTipsLabel];
    [self.contentView addSubview:self.endTimeLabel];
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.font = [UIFont systemFontOfSize:15.f];
        _priceLabel.textColor = [UIColor whiteColor];
    }
    return _priceLabel;
}


- (UILabel *)salePriceLabel{
    if (!_salePriceLabel) {
        _salePriceLabel = [[UILabel alloc] init];
        _salePriceLabel.backgroundColor = [UIColor clearColor];
        _salePriceLabel.font = [UIFont systemFontOfSize:13.f];
        _salePriceLabel.textColor = [UIColor whiteColor];
    }
    return _salePriceLabel;
}

- (UILabel *)endTipsLabel{
    if (!_endTipsLabel) {
        _endTipsLabel = [[UILabel alloc] init];
        _endTipsLabel.backgroundColor = [UIColor clearColor];
        _endTipsLabel.font = [UIFont systemFontOfSize:13.f];
        _endTipsLabel.textColor = [UIColor whiteColor];
    }
    return _endTipsLabel;
}

- (UILabel *)endTimeLabel{
    if (!_endTimeLabel) {
        _endTimeLabel = [[UILabel alloc] init];
        _endTimeLabel.backgroundColor = [UIColor clearColor];
        _endTimeLabel.font = [UIFont systemFontOfSize:15.f];
        _endTimeLabel.textColor = [UIColor whiteColor];
    }
    return _endTimeLabel;
}


@end
